<?php
namespace Admin\Controller;
use Common\Controller\BaseController;
use Common\Model\BulidingModel;
use Common\Model\RoomModel;

class RoomController extends BaseController{

	public function _initialize(){
		parent::_initialize();
		self::$_action_block = 8;
		$this->assign('block',self::$_action_block);
	}

	public function rindex(){

		$this->action = 1;
		$name = I('condition');
		$where = !empty($name) ? " locate(room_name,'{$name}') or room_id in(".intval($name).")": ' 1 ';
		$list = D('Room')->where($where)->select();
//		echo D('Room')->_sql();
//		dump($list);exit;
		$this->assign('list',$list);
		$this->assign('type',$this->action);
		$this->display('rindex');
	}

	public function bindex(){

		$this->action = 2;
		$name = I('condition');
		$where = !empty($name) ? " locate(building_name,'{$name}') or building_id in(".intval($name).")": ' 1 ';
		$list = D('Building')->where($where)->select();
//		echo D('Room')->_sql();
//		dump($list);exit;
		$this->assign('list',$list);
		$this->assign('type',$this->action);
		$this->display('bindex');
	}

	/**
	 * 添加管理员,显示添加页面
	 */
	public function add_room(){
		$this->action = 2;
		$bulidingModel = new BulidingModel();
		$buliding = $bulidingModel->getBuliding();
		$this->assign('building',$buliding);
		$this->assign('type',$this->action);
		$this->display('add_room');

	}
	public function add_bulid(){
		$this->action = 3;
		$this->assign('type',$this->action);
		$this->display('add_bulid');

	}
	/**
	 * 修改管理员信息，显示修改页面
	 */
	public function edit(){

		$id = $this->_get('id');

		$id = intval($id);
		$info  = D('AdminUsers')->getInfoById($id);
		if(empty($info)){
			$this->error('数据不存在');
		}
		$this->assign('info',$info);
		$this->display('edit');
	}

	/**
	 * 删除管理员
	 */
	public function delete(){

		$id = $this->_get('id');
		$id = intval($id);
		$info  = D('AdminUsers')->getInfoById($id);
		if(empty($info)){
			$this->error('数据不存在');
		}
		$res = D('AdminUsers')->where('id='.$id)->delete();
		if(false !==$res){
			$this->success('删除成功',U('/User/getAdmin/'));
		}else{
			$this->error('删除失败');
		}
	}
	/**
	 * 保存添加修改数据
	 */
	public function save_room(){
		$post = I('post.');
		$roomModel = new RoomModel();
//		$res  = $roomModel->addRoom($post);
		$res  = D('Room')->addRoom($post);

		if($res['status']){
			$this->success($res['info'],U('Admin/Room/rindex/'));
		}else{
			$this->error($res['info']);
		}
	}

	public function save_bulid(){
		$post = I('post.');
		$roomModel = new BulidingModel();
		$res  = $roomModel->addBuliding($post);

		if($res['status']){
			$this->success($res['info'],U('Admin/Room/bindex/'));
		}else{
			$this->error($res['info']);
		}
	}

	/**
	 * 修改密码
	 */
	public function pass(){
		$data = $this->_post();

		if(empty($data)){
			$this->display('pass');
			exit;
		}

		$res  = D('Users')->changePass($data);

		if($res['status']){
			$this->success($res['info']);
		}else{
			$this->error($res['info']);
		}
	}
}
